﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NapCardOnline.Models
{
    public partial class Payment
    {
        public string ServiceCostUrl { get; set; }
        public string CostName { get; set; }
        public double? CostAmount { get; set; }
        public string ServiceName { get; set; }
        public string ServiceNo { get; set; }
        /// <summary>
        /// Gets the by service cost id and user id.
        /// </summary>
        /// <param name="serviceCostId">The service cost id.</param>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static Payment ShowPaymentByServiceCostIdAndUserId(int serviceCostId, int userId)
        {
            using (var db = new NapCardOnlineEntities())
            {
                var objPayment = new Payment();
                var objServiceCost = db.ServiceCosts.Include("Service").Include("Cost").FirstOrDefault(s => s.ServiceCostID==serviceCostId);
                if (objServiceCost != null)
                {
                    var objCommissionLevelId = Models.CommissionLevel.GetCommissionLevelIdByUserId(userId);
                    if(objCommissionLevelId>0)
                    {
                        var objCommission =
                         Models.Commission.GetCommissionByServiceIdAndCommissionLevelId(objServiceCost.Service.ServiceID,
                                                                                        objCommissionLevelId);
                        if (objCommission != null)
                        {
                            objPayment.FK_ServiceID = objServiceCost.Service.ServiceID;
                            objPayment.FK_CostID = objServiceCost.Cost.CostID;
                            objPayment.FK_UserID = userId;
                            objPayment.CostName = objServiceCost.Cost.CostName;
                            objPayment.CostAmount = objServiceCost.Cost.CostPrice;
                            objPayment.PaymentAmount = objPayment.CostAmount;
                            objPayment.ServiceName = objServiceCost.Service.ServiceName;
                            objPayment.ServiceNo = objServiceCost.Service.ServiceNo;
                            objPayment.PaymentCommissionPercent = objCommission.CommissionPercent;
                            objPayment.PaymentCommissionAmount = objServiceCost.Cost.CostPrice *
                                                                 objCommission.CommissionPercent / 100;
                            objPayment.IsActive = true;
                            return objPayment;
                        } 
                    }
                    
                }
            }
            return null;

        }
    }
}